package com.jzxk.service.user;

import com.jzxk.model.UserModel;
import com.jzxk.mybatis.dao.UserDao;
import com.jzxk.mybatis.entity.User;
import com.jzxk.mybatis.utils.EncodeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 今朝惜客
 * @CreateTime : 16/9/20 下午4:08
 * @Description : XXX
 */

@Service("userService")
public class IUserServiceImpl implements IUserService{

	private static final Logger LOGGER= LoggerFactory.getLogger(IUserServiceImpl.class);

	@Autowired
	private UserDao userDao;

	public UserModel getUserById(Long id) {

		LOGGER.info("开始mybatis查询数据...");

		User user=this.userDao.selectByPrimaryKey(id);

		LOGGER.info("mybatis查询数据结束...");


		if (user!=null){

			UserModel model=new UserModel();
			model.setId(user.getId());
			model.setUsername(user.getUsername());
			model.setPassword(user.getPassword());

			return model;
		}
		return null;
	}

	public void updateUser(UserModel model) {

		User user=new User();
		user.setId(model.getId());
		user.setUsername(model.getUsername());
		user.setPassword(EncodeUtils.md5Encode(model.getPassword()));

		LOGGER.info("开始mybatis更新数据...");

		this.userDao.updateByPrimaryKeySelective(user);

		LOGGER.info("mybatis更新数据结束...");

	}

	public void insertUser(UserModel userModel) {

		User user=new User();
		user.setUsername(userModel.getUsername());
		user.setPassword(EncodeUtils.md5Encode(userModel.getPassword()));
		this.userDao.insert(user);
	}

}
